package com.soyotec.algorithm.util;

import cn.hutool.core.io.IoUtil;

import java.io.*;

public class OPTFileUtil {

    /**
     * 将某个文件copy到某个文件夹下
     * @param srcFile
     * @param workDir
     */
    public static void copyFileToDir(File srcFile, File workDir){
        String fileName = srcFile.getName();
        String tarFilePath = workDir.getAbsolutePath() + File.separator + fileName;
        File tarFile = new File(tarFilePath);
        if(!tarFile.exists()){
            try {
                tarFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(srcFile);
            outputStream = new FileOutputStream(tarFile);
            IoUtil.copy(inputStream,outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 将某个文件夹中的全部文件copy到另一个文件夹中
     * @param srcDir
     * @param targetDir
     */
    public static void copyDirToDir(File srcDir,File targetDir){
        for(File subFile : srcDir.listFiles()){
            if(subFile.isDirectory()){
                String fileName = subFile.getName();
                String tarFilePath = targetDir.getAbsolutePath() + File.separator + fileName;
                File tarDir = new File(tarFilePath);
                if(!tarDir.exists()){
                    tarDir.mkdirs();
                }
                copyDirToDir(subFile,tarDir);
            } else if(subFile.isFile()){
                copyFileToDir(subFile,targetDir);
            }
        }
    }

    public static void copyFilesToDir(File[] srcFiles,File targetDir){
        for(File subFile : srcFiles){
            if(subFile.isDirectory()){
                String fileName = subFile.getName();
                String tarFilePath = targetDir.getAbsolutePath() + File.separator + fileName;
                File tarDir = new File(tarFilePath);
                if(!tarDir.exists()){
                    tarDir.mkdirs();
                }
                copyDirToDir(subFile,tarDir);
            } else if(subFile.isFile()){
                copyFileToDir(subFile,targetDir);
            }
        }
    }

    public static File findMainFile(File[] otherFiles, String fileName) {
        if(otherFiles != null){
            for (File file : otherFiles) {
                if (file.isDirectory()) {
                    File[] subFiles = file.listFiles();
                    File mainFile = findMainFile(subFiles,fileName);
                    if(mainFile != null){
                        return mainFile;
                    }
                } else if(file.isFile()){
                    if (file.getName().equals(fileName)) {
                        return file;
                    }
                }
            }
        }
        return null;
    }

    public static File findMainFile(File dir, String fileName){
        if(dir.isDirectory()){
            return findMainFile(dir.listFiles(),fileName);
        }
        return null;
    }

    public static void main(String[] args){
        double[] vars = new double[]{1.60497,-0.442824};
        double x = vars[0];
        double y = vars[1];
        double obj1 = 4 * x * x + 4 * y * y;
        double obj2 = Math.pow((x-5),2) + Math.pow((y-5),2);

        double con1 = Math.pow((x-5),2) + y * y;
        double con2 = Math.pow((x-8),2) + Math.pow((y+3),2);

        System.out.println("con1:" + con1);
        System.out.println("con2:" + con2);
        System.out.println("obj1:" + obj1);
        System.out.println("obj2:" + obj2);

    }
}
